package pt.isel.lercm.projectmanager.managedController.resume;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;

import pt.isel.lercm.projectmanager.auxobjects.Mode;
import pt.isel.lercm.projectmanager.spring.commons.GenericManagedBeanResume;
import pt.isel.lercm.projectmanager.spring.model.Aluno;
import pt.isel.lercm.projectmanager.spring.service.IAlunoService;
import pt.isel.lercm.projectmanager.spring.service.IConstanteService;


@ManagedBean(name = "alunoResumeBean")
@ViewScoped
public class AlunoResume extends GenericManagedBeanResume<Aluno> implements Serializable{

	private static final long serialVersionUID = 7487798232349750869L;

	private List<Aluno> alunos;

	@ManagedProperty(value="#{AlunoService}")
	private IAlunoService alunoService;

	@ManagedProperty(value="#{ConstanteService}")
	private IConstanteService constanteService;
	
	@Override
	public void init() {
		if (alunos == null){
			if (alunoService.getAllByYear(Integer.parseInt(constanteService.getValorByName("ANO_ACTUAL"))) != null) {
				alunos = new ArrayList<Aluno>(alunoService.getAllByYear(Integer.parseInt(constanteService.getValorByName("ANO_ACTUAL"))));
			}
			else{
				alunos = new ArrayList<Aluno>();
			}
		}
	}
	
	@Override
	public void novo() {
		redirectToPage("/app/crud/alunoCrud.xhtml?mode=" + Mode.NOVO.mode,false);
	}
	
	@Override
	public void visualizar(Aluno object) {
		redirectToPage("/app/crud/alunoCrud.xhtml?id="+ object.getId() +"&mode=" + Mode.LEITURA.mode,false);
	}
	
	@Override
	public void apagar(Aluno object) {
		alunos.remove(object);
		
		alunoService.delete(object);
		
		FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "O aluno foi apagado com sucesso!", ""));
		
	}
	
	public List<Aluno> getAlunos() {
		return alunos;
	}

	public void setAlunos(List<Aluno> alunos) {
		this.alunos = alunos;
	}
	
	public IAlunoService getAlunoService() {
		return alunoService;
	}

	public void setAlunoService(IAlunoService alunoService) {
		this.alunoService = alunoService;
	}

	public IConstanteService getConstanteService() {
		return constanteService;
	}

	public void setConstanteService(IConstanteService constanteService) {
		this.constanteService = constanteService;
	}
}
